package MainControl;

import Tool.GlobalVariable;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class CommandControl implements Runnable {
    private ServerSocket getCommandServer;
    private Socket getCommandSocket;
    private InputStream getCommandStream;
    private volatile static CommandControl commandControl;
    private static List<String> changeList = new ArrayList<>();

    private CommandControl() {
        try {
            getCommandServer = new ServerSocket(7001);
            getCommandSocket = getCommandServer.accept();
            getCommandStream = getCommandSocket.getInputStream();
            System.out.println("InitFinish成功！");
        } catch (IOException e) {
            System.out.println("InitFinish失败！");
            e.printStackTrace();
        }
    }

    public static CommandControl getInstance() {
        if (commandControl == null) {
            synchronized (CommandControl.class) {
                if (commandControl == null) {
                    commandControl = new CommandControl();
                }
            }
        }
        return commandControl;
    }

    @Override
    public void run() {
        while (true) {
            try {
                byte[] commandBytes = new byte[1024];
                getCommandStream.read(commandBytes);
                if (commandBytes.length > 0) {
                    String command = (new String(commandBytes)).trim();
                    String commandHead = command.split(":")[0].trim();

                    if (commandHead.equals("STU"))//接收到的是设置测试台位命令SettingTestUnit
                    {
                        System.out.println(command);
                        if (command.split(":").length != 1) {
                            String commandBody = command.split(":")[1];
                            String testingUnits[] = commandBody.split(",");
                            GlobalVariable.testingUnitsID.clear();
                            for (int i = 0; i < testingUnits.length; i++) {
                                //0位台位号，1位洗衣机类型
                                String testUnitAndModelType[] = testingUnits[i].split("#");
                                GlobalVariable.testingUnitsID.put(Integer.parseInt(testUnitAndModelType[0]), testUnitAndModelType[1]);
                                System.out.println(GlobalVariable.testingUnitsID.get(Integer.parseInt(testUnitAndModelType[0])));
                                MainControl.getMainControl().reInit(Integer.parseInt(testUnitAndModelType[0]));
                            }
                        }
                    }
                    if (commandHead.equals("START")) {
                        System.out.println(command);
                        String commandTaiweiNo = command.split(":")[1].trim();
                        String testUnitsAndModelType[] = commandTaiweiNo.split("#");
                        GlobalVariable.testingUnitsID.put(Integer.parseInt(testUnitsAndModelType[0]), testUnitsAndModelType[1]);
                        MainControl.getMainControl().reInit(Integer.parseInt(testUnitsAndModelType[0]));

                    }
                    if (commandHead.equals("STOP")) {
                        System.out.println(command);
                        String commandTaiweiNo = command.split(":")[1].trim();
                        String testUnitsAndModelType[] = commandTaiweiNo.split("#");
                        GlobalVariable.collectionThreads.get(Integer.parseInt(testUnitsAndModelType[0])).TerminateThread();
                        GlobalVariable.testingUnitsID.remove(Integer.parseInt(testUnitsAndModelType[0]));
                        GlobalVariable.runningThread.remove(Integer.parseInt(testUnitsAndModelType[0]));
                    }
                    //设置结束后关闭采集
                    synchronized (this) {
                        if (commandHead.equals("END")) {
                            System.exit(0);
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
